package com.byapp.fast.common.converter;


import com.byapp.fast.modules.match.dto.MatchOddsDTO;
import com.byapp.fast.modules.match.entity.AsianOddsEntity;
import com.byapp.fast.modules.match.entity.BaseOddsEntity;
import com.byapp.fast.modules.match.entity.EuropeOddsEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class MatchOddsDTOConverter implements ConverterFactory<MatchOddsDTO, BaseOddsEntity> {


    @Override
    public MatchOddsDTO create(BaseOddsEntity baseOddsEntity) {
        if (baseOddsEntity instanceof AsianOddsEntity) {
            AsianOddsEntity entity = (AsianOddsEntity) baseOddsEntity;
            String odds = String.format("%s,%s", entity.getFixtrueWin(), entity.getFixtrueLost());
            return new MatchOddsDTO().setMsi(entity.getMatchSpId())
                    .setIsMaster(entity.getIsMaster())
                    .setPlayId(entity.getPlayId())
                    .setOdds(odds)
                    .setHandicap(entity.getHandicapValue());
        }
        if (baseOddsEntity instanceof EuropeOddsEntity) {
            EuropeOddsEntity entity = (EuropeOddsEntity) baseOddsEntity;
            String odds = String.format("%s,%s,%s", entity.getFixtrueWin(), entity.getFixtrueDraw(), entity.getFixtrueLost());
            return new MatchOddsDTO().setMsi(entity.getMatchSpId())
                    .setIsMaster(entity.getIsMaster())
                    .setPlayId(entity.getPlayId())
                    .setOdds(odds);
        }
        return null;
    }

    static List<MatchOddsDTO> mapToTarget(List<BaseOddsEntity> list) {
        return !CollectionUtils.isEmpty(list) ? list.stream()
                .map(new MatchOddsDTOConverter()::create)
                .collect(Collectors.toList()) : null;
    }
}
